package com.cuit.jifver.weibo.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuit.jifver.weibo.R;
import com.cuit.jifver.weibo.model.WeiBoData;
import com.cuit.jifver.weibo.model.sina.Status;
import com.cuit.jifver.weibo.utils.ConfigUtil;
import com.cuit.jifver.weibo.utils.Constants;
import com.cuit.jifver.weibo.utils.LogUtil;
import com.cuit.jifver.weibo.widget.RemoteCacheImageView;

public class ListViewAdapter<T> extends BaseAdapter
{
	private List<T> list;
	LayoutInflater inflater;
	private SimpleDateFormat format = new SimpleDateFormat(Constants.DATE_FORMAT);
	private SimpleDateFormat sinaDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy",Locale.ENGLISH);
	String weibo = ConfigUtil.getInstance().getCurWeibo();

	public ListViewAdapter(List<T> list, Context context) {
		this.list = list;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list == null ? 0:list.size();
	}

	@Override
	public Object getItem(int position) {
		return list == null ? null:list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		View view = inflater.inflate(R.layout.weibo_item, null);
		
		
		RemoteCacheImageView headImg = (RemoteCacheImageView) view.findViewById(R.id.img_head);
		ImageView isVipImg = (ImageView) view.findViewById(R.id.img_isvip);
		ImageView commentImg = (ImageView) view.findViewById(R.id.img_comment);
		ImageView forwardImg = (ImageView) view.findViewById(R.id.img_forward);
		ImageView timeImg = (ImageView) view.findViewById(R.id.img_time);
		
		TextView nickTv = (TextView) view.findViewById(R.id.tv_nick);
		TextView timeTv = (TextView) view.findViewById(R.id.tv_time);
		TextView textTv = (TextView) view.findViewById(R.id.tv_text);
		TextView fromTv = (TextView) view.findViewById(R.id.tv_from);
		TextView forwardTv = (TextView) view.findViewById(R.id.tv_forward);
		TextView commentTv = (TextView) view.findViewById(R.id.tv_comment);
		
		String nick = null;
		int isVip = 0;
		long time = 0;
		int count = 0;   //微博被转次数
		int mcount = 0; //点评次数
		String from = null;
		String text = null;
		
		if (weibo.equals(ConfigUtil.QQW))
		{
			WeiBoData data = (WeiBoData) list.get(position);
			nick = data.getNick();
			isVip = data.getIsVip();
			time = data.getTimestamp();
			count = data.getCount();   //微博被转次数
			mcount = data.getMcount(); //点评次数
			from = data.getFrom();
			text = data.getOrigtext();
		}
		if (weibo.equals(ConfigUtil.SINAW))
		{
			Status data = (Status) list.get(position);
			nick = data.getUser().getScreenName();
			isVip = 0;
			try
			{
				time = sinaDateFormat.parse(data.getCreated_at()).getTime();
				
			}
			catch (ParseException e)
			{
				LogUtil.e(this, "Time parse error");
				e.printStackTrace();
			}
			count = 0;   //微博被转次数
			mcount = 0; //点评次数
			from = "Android";
			text = data.getText();
		}
		
		
		
		timeImg.setVisibility(View.GONE);
		nickTv.setText(nick);
		if (isVip == 0)
		{
			isVipImg.setVisibility(View.GONE);
		}
		timeTv.setText(format.format(new Date(time)));
		if (count == 0)
		{
			forwardImg.setVisibility(View.GONE);
			forwardTv.setVisibility(View.GONE);
		}
		else 
		{
			forwardTv.setText(count+"");
		}
		
		if (mcount == 0)
		{
			commentImg.setVisibility(View.GONE);
			commentTv.setVisibility(View.GONE);
		}
		else 
		{
			commentTv.setText(mcount+"");
		}
		
		fromTv.setText("来自"+from);
		textTv.setText(text);
		
		return view;
	}

}
